﻿using System.Threading;
using System.Windows.Forms;
using AsyncLibrary.StatusListener;

namespace AsyncLibrary.UI
{
  public class StatusFormDecorator : SyncedStatusListener
  {
    private readonly IWin32Window _owner;
    private readonly StatusForm _statusForm;
    private readonly AsyncOperation _op;

    public StatusFormDecorator(IWin32Window owner, StatusForm form, AsyncOperation op) 
      : base (form, SynchronizationContext.Current)
    {
      _owner = owner;
      _statusForm = form;
      _op = op;
      op.OperationStarted += OnOperationStarted;
      op.OperationFinished += OnOperationFinished;
    }

    protected void OnOperationFinished()
    {
      _statusForm.Hide();
      _op.OperationStarted -= OnOperationStarted;
      _op.OperationFinished -= OnOperationFinished;
    }

    protected void OnOperationStarted()
    {
      _statusForm.ShowDialog(_owner);
    }
  }
}